package student.thread;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import com.mysql.jdbc.Statement;

import student.Dao.StudentDaoImpl;
public class ThreadIOImpl extends Thread {
	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	private final ReentrantLock lock = new ReentrantLock();
	String URL = "jdbc:mysql://127.0.0.1:3306/stu";
	String USER = "root";
	String PASSWORD = "123456";
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;

	public void connetion(Connection con) throws SQLException, ClassNotFoundException {
		lock.lock();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
			// st = (Statement) conn.createStatement();
			st = (Statement) conn.prepareStatement("INSERT INTO STU VALUE(NULL,?,?,?)");
			conn = con;
			this.logger.info(getName()+"线程连接数据库成功！");
		} finally {
			//rs.close();
			st.close();
			lock.unlock();
		}

	}

	public void leadingIn(String fileName)throws FileNotFoundException, ClassNotFoundException, IOException, InvalidFormatException {
		StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
		studentDaoImpl.leadingIn(fileName);

	}

	public void run() {
		try {
			conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
			this.connetion(conn);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
